<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- gzip | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- gzip</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;减少文件大小有两个明显的好处，一是可以减少存储空间，二是通过网络传输文件时，可以减少传输的时间。gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令，既方便又好用。gzip不仅可以用来压缩大的、较少使用的文件以节省磁盘空间，还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计，gzip命令对文本文件有60%～70%的压缩率。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;gzip 不能压缩目录</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip [参数] [文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;gzip 是个使用广泛的压缩程序，文件经它压缩后，其名称后面会多出 “.gz” 的扩展名。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><ul>
<li>-a或–ascii 　使用ASCII文字模式。</li>
<li>-c或–stdout或–to-stdout 　把压缩后的文件输出到标准输出设备，不去更动原始文件。 </li>
<li>-d或–decompress或—-uncompress 　解开压缩文件。</li>
<li>-f或–force 　强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。 </li>
<li>-h或–help 　在线帮助。 </li>
<li>-l或–list 　列出压缩文件的相关信息。</li>
<li>-L或–license 　显示版本与版权信息。 </li>
<li>-n或–no-name 　压缩文件时，不保存原来的文件名称及时间戳记。 </li>
<li>-N或–name 　压缩文件时，保存原来的文件名称及时间戳记。 </li>
<li>-q或–quiet 　不显示警告信息。</li>
<li>-r或–recursive 　递归处理，将指定目录下的所有文件及子目录一并处理。 </li>
<li>-S&lt;压缩字尾字符串&gt;或—-suffix&lt;压缩字尾字符串&gt; 　更改压缩字尾字符串。</li>
<li>-t或–test 　测试压缩文件是否正确无误。</li>
<li>-v或–verbose 　显示指令执行过程。 </li>
<li>-V或–version 　显示版本信息。</li>
<li>-num 用指定的数字num调整压缩的速度，-1或–fast表示最快压缩方法（低压缩比），-9或–best表示最慢压缩方法（高压缩比）。系统缺省值为6。</li>
</ul>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4.使用实例"></a>4.使用实例</h2><h3 id="实例1-把-test6-目录下的每个文件压缩成-gz-文件"><a href="#实例1-把-test6-目录下的每个文件压缩成-gz-文件" class="headerlink" title="实例1.把 test6 目录下的每个文件压缩成 .gz 文件"></a>实例1.把 test6 目录下的每个文件压缩成 .gz 文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip *</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># gzip *</span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 28</div><div class="line">---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz</div><div class="line">---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz</div><div class="line">-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h2 id="实例2：把例1中每个压缩的文件解压，并列出详细的信息"><a href="#实例2：把例1中每个压缩的文件解压，并列出详细的信息" class="headerlink" title="实例2：把例1中每个压缩的文件解压，并列出详细的信息"></a>实例2：把例1中每个压缩的文件解压，并列出详细的信息</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -dv *</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 28</div><div class="line">---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz</div><div class="line">---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz</div><div class="line">-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz</div><div class="line">[root@localhost test6]<span class="comment"># gzip -dv *</span></div><div class="line">linklog.log.gz:  99.6% -- replaced with linklog.log</div><div class="line">log2012.log.gz:  99.6% -- replaced with log2012.log</div><div class="line">log2013.log.gz:  47.5% -- replaced with log2013.log</div><div class="line">log2014.log.gz:   0.0% -- replaced with log2014.log</div><div class="line">log2015.log.gz:   0.0% -- replaced with log2015.log</div><div class="line">log2016.log.gz:   0.0% -- replaced with log2016.log</div><div class="line">log2017.log.gz:   0.0% -- replaced with log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：详细显示例1中诶每个压缩的文件的信息，并不解压"><a href="#实例3：详细显示例1中诶每个压缩的文件的信息，并不解压" class="headerlink" title="实例3：详细显示例1中诶每个压缩的文件的信息，并不解压"></a>实例3：详细显示例1中诶每个压缩的文件的信息，并不解压</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -l *</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># gzip -l *</span></div><div class="line">         compressed        uncompressed  ratio uncompressed_name</div><div class="line">               1341              302108  99.6% linklog.log</div><div class="line">               1341              302108  99.6% log2012.log</div><div class="line">                 70                  61  47.5% log2013.log</div><div class="line">                 32                   0   0.0% log2014.log</div><div class="line">                 32                   0   0.0% log2015.log</div><div class="line">                 32                   0   0.0% log2016.log</div><div class="line">                 32                   0   0.0% log2017.log</div><div class="line">               2880              604277  99.5% (totals)</div></pre></td></tr></table></figure>
<h3 id="实例4：压缩一个-tar-备份文件，此时压缩文件的扩展名为-tar-gz"><a href="#实例4：压缩一个-tar-备份文件，此时压缩文件的扩展名为-tar-gz" class="headerlink" title="实例4：压缩一个 tar 备份文件，此时压缩文件的扩展名为 .tar.gz"></a>实例4：压缩一个 tar 备份文件，此时压缩文件的扩展名为 .tar.gz</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -r log.tar</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -al log.tar</span></div><div class="line">-rw-r--r-- 1 root root 307200 11-29 17:54 log.tar</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -r log.tar</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -al log.tar.gz </span></div><div class="line">-rw-r--r-- 1 root root 1421 11-29 17:54 log.tar.gz</div></pre></td></tr></table></figure>
<h3 id="实例5L递归的压缩目录"><a href="#实例5L递归的压缩目录" class="headerlink" title="实例5L递归的压缩目录"></a>实例5L递归的压缩目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -rv test6</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -rv test6</span></div><div class="line">test6/log2014.log:        0.0% -- replaced with test6/log2014.log.gz</div><div class="line">test6/linklog.log:       99.6% -- replaced with test6/linklog.log.gz</div><div class="line">test6/log2015.log:        0.0% -- replaced with test6/log2015.log.gz</div><div class="line">test6/log2013.log:       47.5% -- replaced with test6/log2013.log.gz</div><div class="line">test6/log2012.log:       99.6% -- replaced with test6/log2012.log.gz</div><div class="line">test6/log2017.log:        0.0% -- replaced with test6/log2017.log.gz</div><div class="line">test6/log2016.log:        0.0% -- replaced with test6/log2016.log.gz</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test6</span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 28</div><div class="line">---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz</div><div class="line">---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz</div><div class="line">-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样，所有 test 下面的文件都变成了 <em>.gz ，目录依然存在，只是目录里面的文件相应变成了 </em>.gz 这就是压缩，和打包不同。因为是对目录操作，所以需要加上 -r 选项，这样也可以对子目录进行递归了。</p>
<h3 id="实例6：递归地解压目录"><a href="#实例6：递归地解压目录" class="headerlink" title="实例6：递归地解压目录"></a>实例6：递归地解压目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -dr test6</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 28</div><div class="line">---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz</div><div class="line">---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz</div><div class="line">-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz</div><div class="line">-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz</div><div class="line">[root@localhost test6]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -dr test6</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test6</span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 604</div><div class="line">---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log</div><div class="line">---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log</div><div class="line">-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log</div><div class="line">-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例7：-lt-数字-gt-自定义压缩率"><a href="#实例7：-lt-数字-gt-自定义压缩率" class="headerlink" title="实例7：-&lt;数字&gt; 自定义压缩率"></a>实例7：-&lt;数字&gt; 自定义压缩率</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -v -9 test.tar</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -v -1 test.tar</span></div><div class="line">test.tar: 88.4% -- replaced with test.tar.gz</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -d test.tar.gz</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># gzip -v -9 test.tar</span></div><div class="line">test.tar: 89.7% -- replaced with test.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩等级，1压缩最差，9压缩最好，6为默认。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/32. Linux 命令- gzip/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
